﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Nova_Common;
using Nova_BLL;
using Nova_DAL;
using Nova_MOD;
using System.Data.Common;

public partial class System_User_UserInfo : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    Nova_MOD.Users Nova_MOD_Users = new Nova_MOD.Users();
    Nova_BLL.Users Nova_BLL_Users = new Nova_BLL.Users();
    protected int iCount = 0;
    protected static string strPwd = "";
    protected static string strdDate = "";
    protected static string strvUserNo = "";


    DbHelper dbHelper = new DbHelper();
    DbCommand cmd = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        LoginLogic.IsLogin();

        if (!IsPostBack)
        {
            BindDepart();
            BindGroup();
            Bind_WareHs();
            Bind();
        }


        //验证TextBox
        //string strJs = "";
        //strJs = "return Validate(";
        //strJs += "'txtvUserNo','用户编号不为空','r'";
        //strJs += ",'txtvUserName','用户名称不为空','r'";
        //strJs += ");";
        //ibtnSubmit.Attributes.Add("onclick", strJs);
    }


    private void BindDepart()
    {
        Nova_BLL.DepartMent Nova_BLL_DepartMent = new Nova_BLL.DepartMent();
        ds = Nova_BLL_DepartMent.DataListvDepartment();
        ddlvDepartment.DataSource = ds;
        ddlvDepartment.DataBind();
    }

    private void Bind_WareHs()
    {
        string strSQL = "SELECT * FROM TB_WareHs";
        cmd = dbHelper.GetSqlStringCommond(strSQL);
        ds = dbHelper.ExecuteDataSet(cmd);
        ddl_WareHs.DataSource = ds;
        ddl_WareHs.DataBind();
    }

    private void BindGroup()
    {
        Nova_BLL.Group Nova_BLL_Group = new Nova_BLL.Group();
        ds = Nova_BLL_Group.DataListGroup();
        ddlvGroup.DataSource = ds;
        ddlvGroup.DataBind();
    }

    protected void Bind()
    {
        HttpCookie cookies = Request.Cookies["vNovaUserNo"];
        ds = Nova_BLL_Users.GetUserDataSetByNo(cookies.Value.ToString());
        if (ds.Tables[0].Rows.Count == 0)
        {
            return;
        }
        strvUserNo = txtvUserNo.Text = ds.Tables[0].Rows[0]["vUserNo"].ToString();
        txtvUserName.Text = ds.Tables[0].Rows[0]["vUserName"].ToString();
        ddlvDepartment.SelectedValue = ds.Tables[0].Rows[0]["vDepartmentNo"].ToString();
        ddlvGroup.SelectedValue = ds.Tables[0].Rows[0]["vGroupNo"].ToString();
        rblvSex.SelectedValue = ds.Tables[0].Rows[0]["vSex"].ToString();
        txtvIpAddress.Text = ds.Tables[0].Rows[0]["vIpAddress"].ToString();
        rbliUserStatus.SelectedValue = ds.Tables[0].Rows[0]["iUserStatus"].ToString();
        strPwd = ds.Tables[0].Rows[0]["vPassWord"].ToString();
        strdDate = ds.Tables[0].Rows[0]["dDate"].ToString();
        ddl_WareHs.SelectedValue = ds.Tables[0].Rows[0]["WID"].ToString();
    }

    /// <summary>
    /// 确定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnSubmit_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            HttpCookie cookies = Request.Cookies["vNovaUserNo"];
            
            Nova_MOD_Users.vUserNo = txtvUserNo.Text.Trim();
            Nova_MOD_Users.vUserName = txtvUserName.Text.Trim();
            Nova_MOD_Users.vDepartmentNo = ddlvDepartment.SelectedValue;
            Nova_MOD_Users.vGroupNo = ddlvGroup.SelectedValue;
            Nova_MOD_Users.vSex = rblvSex.SelectedValue;
            Nova_MOD_Users.vIpAddress = txtvIpAddress.Text.Trim();
            Nova_MOD_Users.iUserStatus = rbliUserStatus.SelectedValue;
            Nova_MOD_Users.vPassWord = strPwd;
            //Nova_MOD_Users.dDate = Convert.ToDateTime(strdDate);

            iCount = Nova_BLL_Users.UpdateUser(Nova_MOD_Users, txtvUserNo.Text.Trim());


            if (iCount == 2)
            {
                ShowBox.MessageBox(this.Page, "用户编号已存在，操作失败！", "UserInfo.aspx");
            }
            else if (iCount == 1)
            {
                ShowBox.MessageBox(this.Page, "操作成功！", "UserInfo.aspx");
            }
            else
            {
                return;
            }

        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }



    /// <summary>
    /// 清空
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnReset_Click(object sender, ImageClickEventArgs e)
    {
        Bind();
    }
}
